/*
*	bootstrap 初始化配置js模块
*   @Author jackson wang
*   @date 2016-09-07
*   @version 1.0
*
*/

define(["jquery","bsFormServ","bsMsgServ"], function($,bsFormServ,bsMsgServ) {

	'use strict';
	
	//当前站点
	var _site,
		
		
		lob_contentsid = 'lob_contents',
		
		//流程节点查询url
		field_url = '/flowTaskConfig/query/getTaskConfig.action',
		
		//业务模块配置缓存
		bizConfig = {},
		
		//页面编码全称配置缓存
		fullBizcodeConfig = {},
		
		//当前登录用户
		currentUser ,
		
		getCurrentUserUrl='/biz/getCurentUser.action',
		
		//定义动态页面容器对象
		container = 'main-container',
		
		//ckeditor配置
		lobConfig
	
	var public_method = {
		
		/**
		 * 获取字段配置
		 */
		getFieldRule:function(_options){
			return private_method.getFieldRule(_options)
		}
		,
		/**
		 * 获取也编码配置
		 * @param _options.bizcode	模块代码
		 *
		 */
		getBizConfig:function(_options){
			if(_options){
				
				if(_options.bizcode){
					if(bizConfig[_options.bizcode]){
						return bizConfig[_options.bizcode]
					}else{
						return private_method.getBizConfig(_options)
					}
				}
			}
		}
		,
		getGridHeight:function(){
			var ret=500;
			var _height = $(document).height();
			if(_height<=500){
				ret = _height  ;
			}
			return ret;
		}
		,
		//设置当前登录用户
		setCurrentUser:function(_options){
			currentUser = _options;
		}
		,
		//获取当前用户
		getCurrentUser:function(){
			if(!currentUser){
				return private_method.getUserInfo();
			}else{
				return currentUser;
			}
		}
		,
		//获取页面站点
		getSite:function(){
			if(_site){
				return _site;
			}else{
				_site = private_method.getSite();
				return _site;
			}
		}
		,
		setSite:function(site){
			if(site){
				_site = site;
			}
		}
		,
		
		//获取ckeditor配置
		getLobConfig:function(){
			return private_method.getLobConfig();
		}
		,
		//设置富文本框内容
		setLobContent:function(_options){
			return private_method.setLobContents(_options)
		}
		,
		
		/**
		 * 获取文档大字段内容
		 * @param lobid 	大字段id
		 */
		getLobContents:function(lobid){
			
			var ret
			
			var _param = {
				url:'/'+public_method.getSite()+'/lob/getOne.action',
				data:{id:lobid},
				async:false,
				success:function(data){
					if(data && data.lob_contents){
						ret = data.lob_contents
					}
				}
				,
				failure:function(){
					bsMsgServ.errorSlide(
						codeDef.getConfig({
							type:'load',code:'error'
						})
					)
				}
			}
			$.ajax(_param);
			return ret;
		}
		,
		
		//获取按钮类型配置
		getBtnType:function(e){
			var retVal
			if(e && e.currentTarget){
				var _el = $(e.currentTarget)
				if(_el.length==1){
					retVal = _el.attr('btn-type')
				}
			}
			
			return retVal;
		}
		
		,
		
		//获取按钮类型配置
		getNavType:function(e){
			var retVal
			if(e && e.currentTarget){
				var _el = $(e.currentTarget)
				if(_el.length==1){
					retVal = _el.attr('id')
				}
			}
			
			return retVal;
		}
		,
		
		/**
		 * 响应按钮事件
		 * @param _options.btnEvt
		 * @param _options.btnConfig
		 */
		applyBtnEvt:function(_options){
			if(_options){
				if(_options.btnConfig && _options.btnEvt ){
					
					//判断按钮禁用状态
					var _target = _options.btnEvt.currentTarget;
					if(_target){
						if($(_target).hasClass('disabled')){
							bsMsgServ.warnSlide('当前无法执行该操作！')
							return 
						}
					}
					
					if(window.console){
						console.log(' button click fired...')
					}
					
					//禁用按钮
					$(_target).addClass('disabled');
					var _btn_type = public_method.getBtnType(_options.btnEvt)
					if(_btn_type){
						_btn_type = $.trim(_btn_type);
						
						var _func = _options.btnConfig[_btn_type]
						if(_func){
							_func(_options.btnEvt);
						}
					}
				}
			}
		}
		,
		/**
		 * 加载容器页面
		 */
		loadContainerUrl:function(url){
			var _c = $('#'+container);
			
			if(_c.length==1 && url){
				_c.empty();
				_c.load('/'+public_method.getSite()+url);
			}
		}
		,
		getWidthHeight:function(){
			return {
				height:700,width:960
			}
		}
	}
	
	var private_method = {
		
		//获取页面站点
		getSite:function(){
			
			var _sitePath = window.location.pathname;
			
			var _sp = _sitePath.split('/');
			
			//设置默认站点
			var _site = 'honest';
			
			if(_sp.length>=2){
				var _site = _sp[1];
				if(_site && _site.indexOf('get')<0){
					
					_site = _sp[1];
				}
			}
			return _site;
		}
		,
		/**
			 * 设置大字段内容
			 * @param _options.edit_window
			 * @param _options.data
			 * @param _options.contentsid
			
			 * @param _options.lob_contentEl
			 * @return 	ckeditor对象实例
			 * 
			 */
			setLobContents:function(_options){
				
				
				var defaultConfig={
					
					data:null,
//					contentsid:contentsid,
					lob_contentsid:lob_contentsid
				}
				
				$.extend(defaultConfig,_options)
				
				if(defaultConfig.data){									
					return private_method.initlob(defaultConfig.data);									
				}else{
					return private_method.initlob();
				}
			}
			,
			//获取ckeditor配置
			getLobConfig:function(){
				//定义路径
				
				if(!lobConfig){					
				
					var uploadListurl = '/upload/query/getListDomain.action',
						imageListurl = '/upload/query/getImageListDomain.action',
						uploadurl='/upload/uploadCkeditor.action',
						
						imageUploadurl = '/upload/uploadCkeditorImage.action';
						
					var _site = public_method.getSite();
					
					var config={
						filebrowserBrowseUrl:'/'+_site+uploadListurl,
						filebrowserImageBrowseUrl:'/'+_site+imageListurl,
						filebrowserUploadUrl:'/'+_site+uploadurl,
						filebrowserImageUploadUrl:'/'+_site+imageUploadurl
					}
					lobConfig = config
				
				}
				
				return lobConfig;
				
			}
			,
			initlob:function(data){
				
				
				//工具栏定义
//				config.toolbar = 'MyToolbar';  
//  
//			    config.toolbar_MyToolbar =  
//			    [  
//			        ['Undo', 'Redo', '-', 'SelectAll', 'RemoveFormat'],  
//			        ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],  
//			        ['Maximize'],  
//			       
//			        ['Format', 'Font', 'FontSize'],  
//			        ['TextColor', 'BGColor'],  
//			        ['Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript', 'Superscript']  
//			    ];  
				//editor1 = CKEDITOR.appendTo(lob_contentsid,config,data)
				var config = private_method.getLobConfig();
				var editor1 = CKEDITOR.replace(lob_contentsid,config,data)
				if(data){
					editor1.setData(data);
				}
				return editor1;
			}
			
			,
			
			/**
			 * 获取业务配置信息
			 * @param _options.bizcode		业务编码
			 * @param _options.fullBizcode	业务全称编码
			 */
			getBizConfig:function(_options){
				
				var ret
				if(_options){				
					
					if(_options.bizcode){
						if(bizConfig[_options.bizcode]){
							return bizConfig[_options.bizcode]	
						}
					}
					
					if(_options.fullBizcode){
						if(fullBizcodeConfig[_options.fullBizcode]){
							return 	fullBizcodeConfig[_options.fullBizcode];
						}
					}
					var _param ={
						url : '/'+private_method.getSite()+'/permission/query/getOneByField.action',
						async:false,
						success:function(data){
							if(data ){
								if(data.bizcode){
									//缓存数据
									bizConfig[data.bizcode]=data;
								}
								if(data.fullBizcode){
									//缓存数据
									fullBizcodeConfig[data.fullBizcode]=data;
								}
							}
							//返回结果
							ret = data;
						},
						failure:function(){
							
						}
						
					}
					
					if(_options.bizcode){
						_param.data = {field:'bizcode',fieldVal:_options.bizcode}
					}else{
						if(_options.fullBizcode){
							_param.data = {field:'fullBizcode',fieldVal:_options.fullBizcode}
						}
					}
					
					$.ajax(_param);
				}
				
				return ret;
				
			}
			,
			/**
			 * 获取字段配置信息
			 * @param _options.bizcode	业务编码
			 * @param _options.activitiId	流程节点id
			 */
			getFieldRule:function(_options){
				
				var ret
				if(_options.activitiId){
					//流程业务
					$.ajax({
						url:'/'+init.getSite()+field_url,
						async:false,
						data:task_param,
						type : 'post',
						success:function(data){
							//缓存数据
							if(data){
								var _data = bsFormServ.generateRule(data);
								var _c = public_method.getBizConfig({bizcode:_options.bizcode})
								if(_c){
									if(!_c.flowFieldConfig){
										_c.flowFieldConfig={};
									}								
									_c.flowFieldConfig[_options.activitiId]=_data
								}
								ret = _data;
							}							
						}
					})
				}else{
					//非流程业务
					
				}
				
				return data;
			}		
			
			,
			
			getUserInfo:function(){				
				var _opt = {
					url : '/'+public_method.getSite()+getCurrentUserUrl,					
					async : false,
					success:function(data){
						currentUser = data;						
					}
				};

				$.ajax(_opt);				
				
				return currentUser;
			}
	}
	
	return public_method;
})
	